package thread.Test;

//Demo.java
class MyThreadG extends Thread{  
 public MyThreadG(String name) {
     super(name);
 }

 public void run(){
     try {
         for (int i=0; i<5; i++) {
             Thread.sleep(3);
             System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
         }
     } catch (InterruptedException e) {
     }
 } 
}; 

class MyDaemon extends Thread{  
 public MyDaemon(String name) {
     super(name);
 }

 public void run(){
     try {
         for (int i=0; i<10000; i++) {
             Thread.sleep(1);
             System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
         }
     } catch (InterruptedException e) {
     }
 } 
}
public class DaemonThreadTest {  
 public static void main(String[] args) {  

     System.out.println(Thread.currentThread().getName()
             +"(isDaemon="+Thread.currentThread().isDaemon()+ ")");

     Thread t1=new MyThreadG("t1");    // 新建t1
     Thread t2=new MyDaemon("t2");    // 新建t2
     t2.setDaemon(true);                // 设置t2为守护线程
     t1.start();                        // 启动t1
     t2.start();                        // 启动t2
 }  
}
/*
 * main(isDaemon=false)
t2(isDaemon=true), loop 0
t1(isDaemon=false), loop 0
t2(isDaemon=true), loop 1
t2(isDaemon=true), loop 2
t1(isDaemon=false), loop 1
t2(isDaemon=true), loop 3
t2(isDaemon=true), loop 4
t2(isDaemon=true), loop 5
t2(isDaemon=true), loop 6
t1(isDaemon=false), loop 2
t2(isDaemon=true), loop 7
t2(isDaemon=true), loop 8
t1(isDaemon=false), loop 3
t2(isDaemon=true), loop 9
t2(isDaemon=true), loop 10
t2(isDaemon=true), loop 11
t2(isDaemon=true), loop 12
t1(isDaemon=false), loop 4

 * */
 